/*   
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ewan.android.droidjockey;

import java.util.HashMap;

import ewan.android.droidjockey.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;

import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.view.Menu;
import android.view.MenuInflater;
/**
 * Main activity: shows media player buttons. This activity shows the media
 * player buttons and lets the user click them. No media handling is done here
 * -- everything is done by passing Intents to our {@link MusicService}.
 * */
public class MainActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener {

	static public Uri SongInfoStoreUri;
	private static final int MY_DATA_CHECK_CODE = 1234;
	
	public static TextToSpeech mTts;

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Context ctx = (Context) this.getApplicationContext();
		if (requestCode == MY_DATA_CHECK_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				// success, create the TTS instance
				mTts = new TextToSpeech(ctx, this);
				mTts.setOnUtteranceCompletedListener(this);
				HashMap<String, String> myHashAlarm = new HashMap();
				myHashAlarm
						.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Ended");
			} else {
				// missing data, install it
				Intent installIntent = new Intent();
				installIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installIntent);

			}
		}
	}
	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		startService(new Intent(MusicService.ACTION_READY));
		Bundle extras = getIntent().getExtras();
		
		SongInfoStoreUri = (savedInstanceState == null) ? null : (Uri) savedInstanceState.getParcelable(SongInfoStoreContentProvider.CONTENT_ITEM_TYPE);
		
		if (extras != null) {
			SongInfoStoreUri = extras.getParcelable(SongInfoStoreContentProvider.CONTENT_ITEM_TYPE);
			System.out.println(SongInfoStoreUri);
		}
		
		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
		
		setContentView(R.layout.mainfragment);
	}
	
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putParcelable(SongInfoStoreContentProvider.CONTENT_ITEM_TYPE, SongInfoStoreUri);
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		System.out.println("Menu Created");
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.mainmenu, menu);
	    return true;
	}
	
	
	public void onBackPressed(){
		Context ctx = (Context) this.getApplicationContext();
		startActivity(new Intent(ctx, SongList.class));
	}

	public void onUtteranceCompleted(String utteranceId) {
		
		Context ctx = (Context) this.getApplicationContext();
		ctx.startService(new Intent(MusicService.ACTION_PLAY));

		
	}

	public void onInit(int status) {
		// TODO Auto-generated method stub
		
	}
	
	
}

